package com.code.monitor.controller;

import com.code.monitor.annotation.Controller;
import com.code.monitor.annotation.RequestMapping;
import com.code.monitor.annotation.RequestParam;
import com.code.monitor.annotation.ResponseBody;
import com.code.monitor.config.ServerConfig;
import com.code.monitor.constants.ResultEntity;
import com.code.monitor.model.PackageInfo;
import com.code.monitor.service.PackageService;
import com.code.monitor.utils.SpringUtils;
import com.code.monitor.utils.UUIDUtils;
import io.netty.handler.codec.http.multipart.FileUpload;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

/**
 * @author ggh
 * @date 2023/5/10
 */
@Controller
@Slf4j
public class PackageController {

    /**
     * 上传jar包
     *
     * @param file
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/upload")
    public ResultEntity upload(@RequestParam("file") FileUpload file, @RequestParam(value = "name") String name) {
        String packageDir = SpringUtils.getBean(ServerConfig.class).getPackageDir();

        String destName = UUIDUtils.uploadId();
        String dest = null;
        try {
            String destDir = packageDir + "/" + destName;
            if (!Files.exists(Paths.get(destDir))) {
                Files.createDirectory(Paths.get(destDir));
            }
            dest = destDir + "/" + file.getFilename();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (FileOutputStream out = new FileOutputStream(dest)) {
            out.write(file.get());
            PackageInfo packageInfo = new PackageInfo();
            packageInfo.setFileName(file.getFilename());
            packageInfo.setUuid(destName);
            packageInfo.setName(name);
            packageInfo.setPath(dest);
            packageInfo.setUploadTime(new Date());
            SpringUtils.getBean(PackageService.class).addPackage(packageInfo);
            return ResultEntity.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultEntity.fail("操作失败");
    }

    /**
     * 列举程序包
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/packageList")
    public ResultEntity packageList() {
        try {
            return ResultEntity.success(SpringUtils.getBean(PackageService.class).queryAllPackage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultEntity.fail("操作失败");
    }

    /**
     * 删除程序包
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delPackage")
    public ResultEntity delPackage(@RequestParam("uuid") String uuid) {
        try {
            PackageInfo packageInfo = SpringUtils.getBean(PackageService.class).queryPackageByUUID(uuid);
            SpringUtils.getBean(PackageService.class).delPackage(uuid);
            Files.deleteIfExists(Paths.get(packageInfo.getPath()));

            return ResultEntity.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultEntity.fail("操作失败");
    }
}
